package ihm.classifier;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import corpus.ActionsTexteFichier;
import corpus.CreateInstance;
import model.Classe;
import model.VariableStatiques;
import weka.core.Instances;

public class ClassifierLeft extends JPanel implements ActionListener {

	private static final long serialVersionUID = -4196156234870016324L;
	private GridBagConstraints c;
	private JLabel foldLabel;
	private JTextField foldText;
	private JButton learnButton;
	private JTextArea affichage;
	private JButton openFileButton;
	private DirectoryChooser dc;
	private Instances toClassify; 

	public ClassifierLeft(JTextArea affichage) {
		this.affichage = affichage;
		setLayout(new GridBagLayout());
		c = new GridBagConstraints();
		createOptionBox();
		createBayesControl();
		createClassifyControl();
	} 


	private void createClassifyControl() {
		createFileChooser();
	}


	private void createFileChooser() {
		dc = new DirectoryChooser();
		openFileButton = new JButton("Sélection DOSSIER");
		openFileButton.addActionListener(this);
		c.gridwidth = 3;
		c.gridx = 1;
		c.gridy = 3;
		add(openFileButton, c);
	}

	private void createBayesControl() {
		foldLabel = new JLabel("Folds : ");
		foldText = new JTextField(Integer.toString(VariableStatiques.NAIF_BAYES.getFolds()));
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridwidth = 2;
		c.gridx = 0;
		c.gridy = 0;
		add(foldLabel, c);
		c.gridwidth = 1;
		c.gridx = 2;
		add(foldText, c);
	}
	private void createOptionBox() {
		learnButton = new JButton("Lancer APPRENTISSAGE");
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridwidth = 3;
		c.gridx = 1;
		c.gridy = 1;
		learnButton.addActionListener(this);
		add(learnButton, c);
	}
	@Override
	public void actionPerformed(ActionEvent e) {
	
		if (e.getSource() == learnButton) {
			VariableStatiques.NAIF_BAYES.setFolds(Integer.valueOf(foldText.getText()));
			VariableStatiques.NAIF_BAYES.train(VariableStatiques.MAIN_INSTANCES);
			try {
				affichage.setText("");
				affichage.append(VariableStatiques.NAIF_BAYES.getEval().toCumulativeMarginDistributionString());
				affichage.append(VariableStatiques.NAIF_BAYES.getEval().toClassDetailsString());
				affichage.append(VariableStatiques.NAIF_BAYES.getEval().toSummaryString());
				affichage.append(VariableStatiques.NAIF_BAYES.getEval().toMatrixString());
				affichage.append("#######################################################\n");
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}
	
		if (e.getSource() == openFileButton) {
			int valid = dc.showOpenDialog(this);
			if (valid == JFileChooser.APPROVE_OPTION) {
				ArrayList<Classe> classes = new ArrayList<Classe>();
				File file = dc.getSelectedFile();
				ActionsTexteFichier.parcoursFichiers(file, classes);
				toClassify = CreateInstance.createInstancesSearch(classes);
				try {
					toClassify = VariableStatiques.STRING_TO_VECTOR.outputSearchFilter(toClassify);
				} catch (Exception e1) {
					e1.printStackTrace();
				}
				for (int i = 0; i < toClassify.numInstances(); i++) {
					double pred = VariableStatiques.NAIF_BAYES.classer(toClassify.instance(i));
					affichage.append("ID: " + i);
					affichage.append(", actual: " + (toClassify.instance(i).classIsMissing() ? "?" : toClassify.classAttribute().value((int) toClassify.instance(i).classValue())));
					affichage.append(", predicted: " + toClassify.classAttribute().value((int) pred));
					affichage.append("\n");
				}
				affichage.append("#######################################################\n");
			}
		}
	}

	private class DirectoryChooser extends JFileChooser {
		public DirectoryChooser() {
			setDialogTitle("Sélection DOSSIER");
			setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		}
	} 
}
